/*
 *  Copyright (C) 2011 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * dg_Password.java
 *
 * Created on 05/06/2011, 02:15:13
 */
package kuasar.plugin.utils.dialogs;

import java.awt.Color;
import java.awt.event.KeyEvent;
import kuasar.plugin.Intercom.GUI;
import kuasar.plugin.utils.Connection;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class dg_Username extends javax.swing.JDialog {
    private String username=null;
    private String address = null;

    public dg_Username(java.awt.Frame parent, boolean modal, String address) {
        super(parent, modal);
        this.address=address;
        initComponents();
        this.getContentPane().setBackground(Color.black);
        this.getRootPane().setOpaque(false);
        if(parent == null){
            setLocation(GUI.getFrameLocation().x + this.getPreferredSize().width/2, 
                    GUI.getFrameLocation().y + this.getPreferredSize().height/2);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pn_Square_Container = new kuasar.plugin.classMod.Panel_Trans_Square();
        pn_Round_Container = new kuasar.plugin.classMod.Panel_Opaque();
        txt_username = new javax.swing.JTextField();
        tb_save = new javax.swing.JToggleButton();
        tb_dnie = new javax.swing.JToggleButton();
        pwd_password = new javax.swing.JPasswordField();
        lbl_username = new javax.swing.JLabel();
        lbl_password = new javax.swing.JLabel();
        lbl_title = new javax.swing.JLabel();
        bt_Cancel = new javax.swing.JButton();
        tgb_top = new javax.swing.JToggleButton();
        bt_Accept = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Password Request");
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(0, 0, 0));
        setResizable(false);
        setUndecorated(true);

        pn_Square_Container.setBackground(new java.awt.Color(0, 0, 0));
        pn_Square_Container.setOpaque(false);

        pn_Round_Container.setOpaque(false);

        txt_username.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        txt_username.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txt_username.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_usernameKeyReleased(evt);
            }
        });

        tb_save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/document-nosave.png"))); // NOI18N
        tb_save.setSelected(true);
        tb_save.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        tb_save.setBorderPainted(false);
        tb_save.setContentAreaFilled(false);
        tb_save.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        tb_save.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/document-save.png"))); // NOI18N

        tb_dnie.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/secure-card-bw.png"))); // NOI18N
        tb_dnie.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        tb_dnie.setBorderPainted(false);
        tb_dnie.setContentAreaFilled(false);
        tb_dnie.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        tb_dnie.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/secure-card.png"))); // NOI18N
        tb_dnie.setVerifyInputWhenFocusTarget(false);
        tb_dnie.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                tb_dnieItemStateChanged(evt);
            }
        });

        pwd_password.setFont(new java.awt.Font("Dialog", 0, 18));
        pwd_password.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pwd_password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                pwd_passwordKeyReleased(evt);
            }
        });

        lbl_username.setForeground(new java.awt.Color(204, 204, 204));
        lbl_username.setText("Username:");

        lbl_password.setForeground(new java.awt.Color(204, 204, 204));
        lbl_password.setText("Password:");

        javax.swing.GroupLayout pn_Round_ContainerLayout = new javax.swing.GroupLayout(pn_Round_Container);
        pn_Round_Container.setLayout(pn_Round_ContainerLayout);
        pn_Round_ContainerLayout.setHorizontalGroup(
            pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_Round_ContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pn_Round_ContainerLayout.createSequentialGroup()
                        .addGroup(pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lbl_username, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbl_password, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pwd_password, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_Round_ContainerLayout.createSequentialGroup()
                        .addComponent(tb_dnie)
                        .addGap(18, 18, 18)
                        .addComponent(tb_save)))
                .addContainerGap())
        );
        pn_Round_ContainerLayout.setVerticalGroup(
            pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_Round_ContainerLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbl_username, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pwd_password, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_password, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pn_Round_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tb_save)
                    .addComponent(tb_dnie))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lbl_title.setFont(new java.awt.Font("Dialog", 1, 20));
        lbl_title.setForeground(new java.awt.Color(204, 204, 204));
        lbl_title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/logged.png"))); // NOI18N
        lbl_title.setText("Insert login");

        javax.swing.GroupLayout pn_Square_ContainerLayout = new javax.swing.GroupLayout(pn_Square_Container);
        pn_Square_Container.setLayout(pn_Square_ContainerLayout);
        pn_Square_ContainerLayout.setHorizontalGroup(
            pn_Square_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_Square_ContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_Square_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pn_Round_Container, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbl_title, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE))
                .addContainerGap())
        );
        pn_Square_ContainerLayout.setVerticalGroup(
            pn_Square_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_Square_ContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_title, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pn_Round_Container, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bt_Cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/exit.png"))); // NOI18N
        bt_Cancel.setBorderPainted(false);
        bt_Cancel.setContentAreaFilled(false);
        bt_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_CancelActionPerformed(evt);
            }
        });

        tgb_top.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/go-down.png"))); // NOI18N
        tgb_top.setSelected(true);
        tgb_top.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        tgb_top.setBorderPainted(false);
        tgb_top.setContentAreaFilled(false);
        tgb_top.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/go-top.png"))); // NOI18N
        tgb_top.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tgb_topStateChanged(evt);
            }
        });

        bt_Accept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/icons/dialog-ok-apply.png"))); // NOI18N
        bt_Accept.setBorderPainted(false);
        bt_Accept.setContentAreaFilled(false);
        bt_Accept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_AcceptActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(393, Short.MAX_VALUE)
                .addComponent(tgb_top)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_Accept, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(pn_Square_Container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pn_Square_Container, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bt_Accept)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(tgb_top, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bt_Cancel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_CancelActionPerformed
        username=null;
        close();
    }//GEN-LAST:event_bt_CancelActionPerformed

    private void tgb_topStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tgb_topStateChanged
        this.setAlwaysOnTop(tgb_top.isSelected());
    }//GEN-LAST:event_tgb_topStateChanged

    private void bt_AcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_AcceptActionPerformed
        accept();
    }//GEN-LAST:event_bt_AcceptActionPerformed

    private void tb_dnieItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_tb_dnieItemStateChanged
        if(tb_dnie.isSelected()){
            txt_username.setText("");
            username="";
        }
        txt_username.setEditable(!tb_dnie.isSelected());
    }//GEN-LAST:event_tb_dnieItemStateChanged

    private void pwd_passwordKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwd_passwordKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            accept();
        }
    }//GEN-LAST:event_pwd_passwordKeyReleased

    private void txt_usernameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_usernameKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            accept();
        }
    }//GEN-LAST:event_txt_usernameKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_Accept;
    private javax.swing.JButton bt_Cancel;
    private javax.swing.JLabel lbl_password;
    private javax.swing.JLabel lbl_title;
    private javax.swing.JLabel lbl_username;
    private javax.swing.JPanel pn_Round_Container;
    private javax.swing.JPanel pn_Square_Container;
    private javax.swing.JPasswordField pwd_password;
    private javax.swing.JToggleButton tb_dnie;
    private javax.swing.JToggleButton tb_save;
    private javax.swing.JToggleButton tgb_top;
    private javax.swing.JTextField txt_username;
    // End of variables declaration//GEN-END:variables
    private void save(){
        if(address!=null)
            Connection.addUserSecret(address, username, pwd_password.getPassword(), tb_dnie.isSelected());
    }
    
    private void accept(){
        username = txt_username.getText().trim();
        if(username.isEmpty() && !tb_dnie.isSelected()) username=null;
        if(tb_save.isSelected())save();
        close();
    }
    public void setHeader(String title){
        lbl_title.setText(title);
    } 
    public String getUserName(){
        return username;
    }
    public char[] getPassword(){
        return pwd_password.getPassword();
    }
    public boolean isDNIe(){
        return tb_dnie.isSelected();
    }
    private void close(){
        this.dispose();
    }
    public void disableSave(){
        tb_save.setSelected(false);
        tb_save.setEnabled(false);
    }
    
    public void disableDNIe(){
        tb_dnie.setSelected(false);
        tb_dnie.setEnabled(false);
    }
    
    public void setData(String username, char[] password){
        if(username!=null){
            this.username = username;
            txt_username.setText(username);
        }
        if(password!=null){
            pwd_password.setText(String.valueOf(password));
        }
    }
}
